#include <stdio.h>
#include <stdlib.h>
# include <string.h>
#include <sys/shm.h>
#include <sys/ipc.h>

// 使用共享内存进行通讯
#define PATH_NAME "."
#define PRO_ID  100
#define SZ 64

int main(int argc, char const *argv[])
{   
    int i = 0;
    key_t key = ftok(PATH_NAME, PRO_ID);
    if (key == -1)
    {
        perror("ftok");
        exit(1);
    }
    int shmid = shmget(key, SZ, IPC_CREAT | 0666);
    // 映射到共享内存
    void *shmaddr = shmat(shmid, NULL, 0);
    char buffer[10] = {0};
    // 将该内存ID数据 拷贝到数组中
    memcpy(buffer, shmaddr,  10);
    
    for (i = 0; i < 10; i++) {
        printf("%c", buffer[i]);
    }
    putchar('\n');
    shmdt(shmaddr);
    return 0;
}
